package com.hrm.action;

import com.hrm.action.annotation.Action;
import com.hrm.action.annotation.RequestPath;
import com.hrm.action.annotation.ResponsePath;
import com.hrm.dto.Envelope;
import com.hrm.entity.AdminEntity;
import com.hrm.entity.EmployeeEntity;
//import com.hrm.entity.EmployerEntity;
import com.hrm.entity.InfoEntity;
import com.hrm.service.DepartmentService;
import com.hrm.service.EmployeeService;
import com.hrm.util.DateUtil;
import net.sf.json.JSONObject;
//import com.hrm.service.EmployerService;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Create by zekdot on 20-2-25.
 */
@Action
@RequestPath(path="/employee")
public class EmployeeAction {

    private EmployeeService employeeService;

    public EmployeeAction(){
        employeeService = new EmployeeService();
    }

    @RequestPath(path="/info")
    @ResponsePath(path="/employee/main")
    public Envelope info(HttpSession session){
        EmployeeEntity employeeEntity = (EmployeeEntity) session.getAttribute("user");
        return employeeService.getInfo(employeeEntity.getId());
    }

    @RequestPath(path = "/login")
    public String login(HttpSession session,String id,String password){
        Envelope envelope = employeeService.login(id,password);
        session.setAttribute("user",envelope.getObj());
        envelope.setObj(null);
        return envelope.toString();
    }

    @RequestPath(path = "/changePasswordPage")
    @ResponsePath(path = "/common/changePassword")
    public String changePasswordPage(){
        return "success";
    }

    @RequestPath(path = "/changePassword")
    public String changePassword(HttpSession session,String oriPassword,String newPassword){
        EmployeeEntity employeeEntity = (EmployeeEntity) session.getAttribute("user");
        return employeeService.changePassword(employeeEntity.getId(),oriPassword,newPassword).toString();
    }
    @RequestPath(path = "/salaryPage")
    @ResponsePath(path = "/employee/salary")
    public Envelope salaryPage(HttpSession session){
        EmployeeEntity employeeEntity = (EmployeeEntity) session.getAttribute("user");
        String str = DateUtil.date2Str(new Date());
        String data[] = str.split("-");
        return employeeService.getSalary(employeeEntity.getId(),data[0],data[1]);
    }
    @RequestPath(path = "/salary")
    public String salary(HttpSession session,String year,String month){
        EmployeeEntity employeeEntity = (EmployeeEntity) session.getAttribute("user");
//        String str = DateUtil.date2Str(new Date());
//        String data[] = str.split("-");
        return employeeService.getSalary(employeeEntity.getId(),year,month).toString();
    }

    @RequestPath(path = "/employeePage")
    @ResponsePath(path = "/employee/employee")
    public String employeePage(){
        return "success";
    }

    @RequestPath(path = "/employee")
    public String employee(String id){
        return employeeService.getInfo(id).toString();
    }

    @RequestPath(path = "/get")
    public String getEmployee(String id){
        return employeeService.getEmployee(id).toString();
    }

    @RequestPath(path = "/add")
    public String addEmployee(HttpSession session,String id,String pos){
        EmployeeEntity employeeEntity = (EmployeeEntity) session.getAttribute("user");
        return employeeService.addEmployee(id,employeeEntity.getDepartment(),pos).toString();
    }

    @RequestPath(path = "/del")
    public String delEmployee(String id){
        return employeeService.deleteEmployee(id).toString();
    }

    @RequestPath(path = "/posPage")
    @ResponsePath(path = "/employee/position")
    public Map<String,Object> posPage(HttpSession session,String id){
        EmployeeEntity employeeEntity = (EmployeeEntity) session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        map.put("employee",employeeService.getEmployee(id));
        map.put("positions",new DepartmentService().getPosByDep(employeeEntity.getDepartment()));
        return map;
    }

    @RequestPath(path = "/transfer")
    public String transfer(String id,String newDep,String newPos){
        return employeeService.transfer(id, newDep, newPos).toString();
    }

    @RequestPath(path = "/leave")
    public String leave(String id){
        return employeeService.leave(id).toString();
    }

    @RequestPath(path = "/editSalaryPage")
    @ResponsePath(path = "/employee/editSalary")
    public Map<String,Object> editSalary(String id){
        String str = DateUtil.date2Str(new Date());
        String data[] = str.split("-");
        Map<String,Object> map = new HashMap<>();
        map.put("salary",employeeService.getSalary(id,data[0],data[1]));
        map.put("employee",employeeService.getEmployee(id));
        return map;
    }

    @RequestPath(path = "/updateSalary")
    public Envelope updateSalary(String id,String paramJson){
        String str = DateUtil.date2Str(new Date());
        String data[] = str.split("-");
        return employeeService.updateSalary(id,data[0],data[1],paramJson);
    }

    @RequestPath(path = "/editInfoPage")
    @ResponsePath(path = "/employee/editInfo")
    public Map<String,Object> editInfo(String id) {
        EmployeeEntity employeeEntity = (EmployeeEntity) employeeService.getEmployee(id).getObj();
        InfoEntity infoEntity = (InfoEntity) employeeService.getInfo(id).getObj();
        Map<String, Object> map = new HashMap<>();
        map.put("employee", employeeEntity);
        map.put("info", infoEntity);
        return map;
    }

    @RequestPath(path = "/updateInfo")
    public String updateInfo(String id,String paramJson){
        return employeeService.updateInfo(id,paramJson).toString();
    }

    @RequestPath(path = "/employers")
    public String getEmployers(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",employeeService.getEmployers().getObj());
        return jsonObject.toString();
    }

    @RequestPath(path = "/employees")
    public String getUnderEmployees(String dep){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",employeeService.getUnderEmployees(dep).getObj());
        return jsonObject.toString();
    }
}
